// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include <windows.h> //required by adll.h
#include "sdk_engine/adll.h"
#include <iostream>
#include <sstream>
#include <string>

#include "Constants.h"
#include "Angle.h"
#include "Vector.h"

using namespace std;

// Creates an angle from the direction a vector points to
// equivalent to vec_to_angle() in GameStudio
Angle::Angle(Vector const& vec)
{
	pan = vec.phi();
	tilt = atan2(vec.z, sqrt(vec.x*vec.x + vec.y*vec.y));
	roll = 0;
}

// Creates an angle from the GameStudio equivalent
Angle::Angle(ANGLE const* rhs)
{
	pan = _FLOAT(rhs->pan) / 180.0 * M_PI;
	tilt = _FLOAT(rhs->tilt) / 180.0 * M_PI;
	roll = _FLOAT(rhs->roll) / 180.0 * M_PI;
}

// Converts an Angle back onto the supplied GameStudio ANGLE pointer
void Angle::toGamestudio(ANGLE * angle)
{
	angle->pan = _VAR(pan * 180.0 / M_PI);
	angle->tilt = _VAR(tilt * 180.0 / M_PI);
	angle->roll = _VAR(roll * 180.0 / M_PI);
}


///Assign operator
Angle& Angle::operator=(Angle const& rhs)
{
	pan = rhs.pan;
	tilt = rhs.tilt;
	roll = rhs.roll;
	return *this;
}


/// Return a string representation of the angle
std::string Angle::toString() const
{
	std::ostringstream oss;
	oss << '(' << pan << " , " << tilt << " , " << roll << ')' << std::ends;
	return oss.str();
}

///Equals
bool operator==(Angle const& a, Angle const& b)
{
	return (a.pan == b.pan) && (a.roll == b.roll) && (a.tilt == b.tilt);
}


///Not equal
bool operator!=(Angle const& a, Angle const& b)
{
	return !((a.pan == b.pan) && (a.roll == b.roll) && (a.tilt == b.tilt));
}


///Print an angle to  a stream
std::ostream& operator<<(std::ostream & os, Angle const& rhs)
{
	os << '(' << rhs.pan << " , " <<rhs.tilt << " , " << rhs.roll << ')';

	return os;
}

